package org.anachronos.jeep.ui.trace;

import org.anachronos.jeep.model.trace.DocumentRoot;
import org.anachronos.jeep.model.trace.Invocation;
import org.anachronos.jeep.model.trace.ThreadCallStack;
import org.eclipse.jface.databinding.viewers.TreeStructureAdvisor;

public class TraceTreeStructureAdvisor extends TreeStructureAdvisor {

	@Override
	public Object getParent(final Object element) {
		if (element instanceof ThreadCallStack) {
			final ThreadCallStack threadCallStack = (ThreadCallStack) element;
			return threadCallStack.eContainer().eContainer();
		}
		if (element instanceof Invocation) {
			final Invocation invocation = (Invocation) element;
			return invocation.eContainer();
		}
		return null;
	}
	
	@Override
	public Boolean hasChildren(Object element) {
		if (element instanceof Invocation) {
			final Invocation invocation = (Invocation) element;
			return invocation.getSqlStatementExecution().size() > 0 ||
				invocation.getMethodInvocation().size() > 0 ||
				invocation.getTransaction().size() > 0;
		}
		if (element instanceof DocumentRoot) {
			final DocumentRoot trace = (DocumentRoot) element;
			return trace.getTrace().getCallStacks().getCallStack().size() > 0;
		}
		return false;
	}
}
